package com.springboot.handler;

import com.springboot.exception.CustomBusinessException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author zz
 * @version 1.0
 * @Description 自定义异常页面
 */
@ControllerAdvice
public class BackgroundExceptionHandler {
    // 处理自定义业务异常，返回自定义错误页面
    @ExceptionHandler(CustomBusinessException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ModelAndView handleCustomBusinessException(CustomBusinessException ex) {
        ModelAndView modelAndView = new ModelAndView("error/business_error");
        modelAndView.addObject("errorMessage", ex.getMessage());
        return modelAndView;
    }

    // 处理其他未被捕获的异常，返回通用错误页面
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ModelAndView handleAllExceptions(Exception ex) {
        ModelAndView modelAndView = new ModelAndView("error/general_error");
        modelAndView.addObject("errorMessage", "An unexpected error occurred: " + ex.getMessage());
        return modelAndView;
    }
}
